#include "XOutput.h"

XOutput &XOutput::init(bool state)
{
  pinMode(_pin, OUTPUT);
  return setState(state);
}

XOutput &XOutput::on(void)
{
  if(_enable)
  {
    digitalWrite(_pin, _onLevel);
  }
  return *this;
}

XOutput &XOutput::off(void)
{
  if(_enable)
  {
    digitalWrite(_pin, !_onLevel);
  }
  return *this;
}

XOutput &XOutput::hold(unsigned long time, unsigned long timeAfterOff)
{
  on();
  _delay(time);
  off();
  _delay(timeAfterOff);
  return *this;
}

XOutput &XOutput::setState(bool newState)
{
  return newState ? on() : off();
}

XOutput &XOutput::reverse(void)
{
  return setState(!_state);
}

XOutput &XOutput::blink(int times, unsigned long holdTime)
{
  for(int i = 0; i < times; i++)
  {
    hold(holdTime, holdTime);
  }
  return *this;
}

bool XOutput::getState(void)
{
  return _state;
}

XOutput &XOutput::enable(void)
{
  _enable = true;
  return *this;
}

XOutput &XOutput::disable(void)
{
  _enable = false;
  return *this;
}
